package cn.wit.summit.chwan.models.dbmanager;

import java.util.List;

import cn.wit.summit.chwan.models.db.NetResultCacheTable;
import cn.wit.summit.chwan.models.greendao.gen.NetResultCacheTableDao;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.LogUtils;

/**
 * Created by Alan on 2018/3/21.
 */

public class NetResultCacheManager {

    private        NetResultCacheTableDao mNetResultCacheTableDao;
    private static NetResultCacheManager  netResultCacheManager;

    private NetResultCacheManager() {
        mNetResultCacheTableDao = MyApplication.getMyApplication().getmNetResultCacheTableDao();
    }

    public static NetResultCacheManager getInstance() {
        if (netResultCacheManager == null) netResultCacheManager = new NetResultCacheManager();
        return netResultCacheManager;
    }

    /**
     * 获取缓存
     */
    public NetResultCacheTable getDataByUrl(String url, String requestStr) {
        if (requestStr == null)
            requestStr = "";
        if (isExist(url, requestStr)) {
            return mNetResultCacheTableDao.queryBuilder()
                    .where(NetResultCacheTableDao.Properties.RequestUrl.eq(url)
                            , NetResultCacheTableDao.Properties.RequestStr.eq(requestStr)).unique();
        } else {
            return null;
        }
    }

    /**
     * 插入
     */
    public void insertData(String url, String requestStr, String data) {
        if (requestStr == null)
            requestStr = "";
        try {
            if (isExist(url, requestStr)) {
                NetResultCacheTable temp = mNetResultCacheTableDao.queryBuilder().where(NetResultCacheTableDao.Properties.RequestUrl.eq(url),
                        NetResultCacheTableDao.Properties.RequestStr.eq(requestStr)).unique();
                temp.setRequestStr(requestStr);
                temp.setResponseStr(data);
                temp.setUpDataTime(System.currentTimeMillis());
                updataData(temp);
            } else {
                NetResultCacheTable table = new NetResultCacheTable(null, url, requestStr, data, System.currentTimeMillis());
                mNetResultCacheTableDao.insertOrReplace(table);
            }
        } catch (Exception e) {
            LogUtils.e("url:" + url + ">>>>" + e.getMessage());
        }
    }

    /**
     * 更新
     */
    public void updataData(NetResultCacheTable table) {
        if (table == null) return;
        try {
            mNetResultCacheTableDao.update(table);
        } catch (Exception e) {
            LogUtils.e(">>>>>>e:" + e);
        }
    }

    /**
     * 是否存在
     */
    public boolean isExist(String downUrl, String requestStr) {
        boolean b = false;
        List<NetResultCacheTable> temp = mNetResultCacheTableDao.queryBuilder()
                .where(NetResultCacheTableDao.Properties.RequestUrl.eq(downUrl),
                        NetResultCacheTableDao.Properties.RequestStr.eq(requestStr)).list();
        if (temp == null || temp.size() == 0) {
            b = false;
        } else {
            if (temp.size() == 1) {
                b = true;
            } else if (temp.size() > 1) {
                for (int i = 0; i < temp.size(); i++) {
                    mNetResultCacheTableDao.delete(temp.get(i));
                }
                b = false;
            }
        }
        return b;
    }
}
